<?php

namespace app\index\controller;
use app\common\CommonController;

class Admin extends CommonController
{
    public function getFile()
    {
        $file_path = input('param.file');
        if (file_exists($file_path)) {
            $fp = fopen($file_path, "r");
            $str = fread($fp, filesize($file_path));
            echo $str = str_replace(PHP_EOL,"<br />",$str);
            fclose($fp);
        }
        exit();
    }
    public function setDebug()
    {
        $operator = input('param.operator');
        cache('debugOperator',explode(',',$operator));
        print_r(cache('debugOperator'));
        exit();
    }
    public function deleteLog()
    {
        $dir = input('param.file');
        $this->clearDir($dir);
        exit("删除成功 {$dir}");
    }
    public function clearDir($dir) {
        if(is_dir($dir)) {
            $op = dir($dir);
        }else{
            if(file_exists($dir)){
                @unlink($dir);
            }
            return true;
        }
        while(false != ($item = $op->read())) {
            if($item == '.' || $item == '..') {
                continue;
            }
            if(is_dir($op->path.'/'.$item)) {
                $this->clearDir($op->path.'/'.$item);
                @rmdir($op->path.'/'.$item);
            } else {
                $res = @unlink($op->path.'/'.$item);
                if(!$res) {
                    return false;
                }
            }
        }
        return true;
    }
    public function getLog()
    {
        $param = input('param.');
        $file_path =RUNTIME_PATH."log".DS.date('Ym').DS.date('d').'.log';
        if(!empty($param['delete'])){
            $this->clearDir($file_path);
        }
        if (file_exists($file_path)) {
            $fp = fopen($file_path, "r");
            $str = fread($fp, filesize($file_path));
            echo $str = str_replace(PHP_EOL,"<br />",$str);
            fclose($fp);
        }
        exit();
    }
}
